<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Example:
 * 
 *		$this->load->library('excel');
 *		$this->excel->init('test.xls');
 *
 *		$this->excel->addSheet('test1');
 *		$this->excel->addTitle(array(
 *			array('content'=>'Title1'),
 *			array('content'=>'Title2','color' => Excel::STYLE_PURPLE,'child'=>array('subTitle1','subTitle2')),
 *			array('content'=>'Title3','color' => Excel::STYLE_GREEN),
 *		),Excel::TITLE_LINE_DOUBLE);
 *		$this->excel->addRow(array(
 *			array('content'=>'1'),
 *			array('content'=>'2'),
 *			array('content'=>'3','color'=>Excel::STYLE_BLUE),
 *			array('content'=>'4'),
 *		));
 *		$this->excel->addSheet('test2');
 *
 *		$this->excel->addTitle(array(
 *			array('content'=>'Title3','color' => Excel::STYLE_GREEN),
 *			array('content'=>'Title3','color' => Excel::STYLE_PURPLE),
 *			array('content'=>'Title3'),
 *		));
 *		$this->excel->addRow(array(
 *			array('content'=>'1'),
 *			array('content'=>'2'),
 *			array('content'=>'3','color'=>Excel::STYLE_BLUE),
 *		));
 */

include APPPATH.'third_party/ExcelWriterXML/ExcelWriterXML.php';

class Excel {

	//static
	const TITLE_LINE_SINGLE = 1;
	const TITLE_LINE_DOUBLE = 2;

	//style
	const STYLE_NORMAL = 'styleNormal';
	const STYLE_GREEN = 'styleGreen';
	const STYLE_PURPLE = 'stylePurple';
	const STYLE_BLUE = 'styleBlue';
	const STYLE_ORANGE = 'styleOrange';
	const STYLE_WHEAT = 'styleWheat';

	//format
	const CELL_WIDTH_DEFAULT = 100;
	const CELL_HEIGHT_DEFAULT = 35;

	//sheet
	protected $_title_line_count = Excel::TITLE_LINE_SINGLE;
	protected $_cell_width = 0;
	protected $_cell_height = 0;

	//excel
	protected $_workbook = null;
	protected $_current_sheet = null;
	protected $_sheet_row_max = 0;
	protected $_sheet_row_title = 0;

	public function __construct(){}

	public function init($file = 'file.xml'){
		$this->_workbook = new ExcelWriterXML($file);
		$this->_current_sheet = null;
		$this->_sheet_row_max = 0;
		$this->_title_line_count = Excel::TITLE_LINE_SINGLE;
		$this->_cell_width = Excel::CELL_WIDTH_DEFAULT;
		$this->_cell_height = Excel::CELL_HEIGHT_DEFAULT;

		$this->_setupColor();
	}

	public function addSheet($sTitle){
		$this->_current_sheet = $this->_workbook->addSheet($sTitle);
		$this->_sheet_row_max = 0;
		$this->_title_line_count = Excel::TITLE_LINE_SINGLE;
		$this->_cell_width = Excel::CELL_WIDTH_DEFAULT;
		$this->_cell_height = Excel::CELL_HEIGHT_DEFAULT;
	}

	public function setCellWidth($width){
		$this->_cell_width = $width;
	}

	public function setCellHeight($height){
		$this->_cell_height = $height;
	}

	public function addTitle($title_arr,$line_count = Excel::TITLE_LINE_SINGLE){
		$col = 1;
		$this->_title_line_count = $line_count;
		if($this->_title_line_count == Excel::TITLE_LINE_SINGLE){
			//single line title
			foreach($title_arr as $item){
				if(!isset($item['color'])) $item['color'] = Excel::STYLE_NORMAL;
				$this->_current_sheet->writeString(1,$col,$item['content'],$item['color']);
				$col++;
			}
			$this->_current_sheet->rowHeight(1,$this->_cell_height);
		}elseif($this->_title_line_count == Excel::TITLE_LINE_DOUBLE){
			//double line title
			foreach($title_arr as $item){
				if(!isset($item['color'])) $item['color'] = Excel::STYLE_NORMAL;

				if(isset($item['child'])){
					$subCol = $col;
					foreach($item['child'] as $title){
						$this->_current_sheet->writeString(2,$subCol,$title,$item['color']);
						$subCol++;
					}
					$subCol--;
					$this->_current_sheet->writeString(1,$col,$item['content'],$item['color']);
					$this->_current_sheet->cellMerge(1,$col,count($item['child'])-1,0);
					$col = $subCol + 1;
				}else{
					$this->_current_sheet->writeString(1,$col,$item['content'],$item['color']);
					$this->_current_sheet->cellMerge(1,$col,0,1);
					$col++;
				}
			}
			$this->_current_sheet->rowHeight(1,$this->_cell_height);
			$this->_current_sheet->rowHeight(2,$this->_cell_height);
		}
		for($i=1;$i<$col;$i++){
			$this->_current_sheet->columnWidth($i,$this->_cell_width);
		}

		$this->_sheet_row_max = $line_count;
		$this->_sheet_row_title = $line_count;
	}

	public function addMultiTitle($title_arr) {
		$col = 1;
		$line_count = 1;
		$depth = array_depth($title_arr);
		foreach($title_arr as $it){
			$col = $this->_setChildTitle($it,$line_count,$col,$depth);
		}
		
		$this->_current_sheet->rowHeight($line_count,$this->_cell_height);
		for($i=1;$i<$col;$i++){
			$this->_current_sheet->columnWidth($this->_cell_width,$i);
		}
		$this->_sheet_row_max = $depth;
		$this->_sheet_row_title = $depth;
	}

	private function _setChildTitle($item,$line_count,$col,$depth) {
		if(!isset($item['color'])) $item['color'] = Excel::STYLE_NORMAL;

		if(isset($item['child'])){
			$o_col = $col;
			foreach($item['child'] as $it){
				$col = $this->_setChildTitle($it,$line_count+1,$col,$depth-1);
			}
			$this->_current_sheet->writeString($line_count,$o_col,$item['content'],$item['color']);
			$this->_current_sheet->cellMerge($line_count,$o_col,$col-$o_col-1,0);
			return $col;
		}else{
			$this->_current_sheet->writeString($line_count,$col,$item['content'],$item['color']);
			$this->_current_sheet->cellMerge($line_count,$col,0,$depth-1);
		}
		return $col+1;
	}

	public function freezePanesTitle() {
		$this->_current_sheet->freezePanes($this->_sheet_row_title,0);
	}

	public function addRow($row_arr){
		$this->_sheet_row_max++;
		foreach($row_arr as $key => $item){
			if(!isset($item['color'])) $item['color'] = Excel::STYLE_NORMAL;
			$this->_current_sheet->writeString($this->_sheet_row_max,$key+1,$item['content'],$item['color']);
			if(strpos($item['content'],'http://') !== false){
				$this->_current_sheet->addURL($this->_sheet_row_max,$key+1,$item['content']);
			}
		}
		$this->_current_sheet->rowHeight($this->_sheet_row_max,$this->_cell_height);
	}

	public function freezePanes($row = 0,$column = 0){
		$this->_current_sheet->freezePanes($row,$column);
	}

	public function generate(){
		$this->_workbook->sendHeaders();
		$this->_workbook->writeData();
		die();
	}

	protected function _setupColor(){
		$format = $this->_workbook->addStyle(Excel::STYLE_NORMAL);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->numberFormat('@');
		$format->border('All',1);

		$format = $this->_workbook->addStyle(Excel::STYLE_GREEN);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->border('All',1);
		$format->numberFormat('@');
		$format->bgColor('limegreen');

		$format = $this->_workbook->addStyle(Excel::STYLE_PURPLE);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->border('All',1);
		$format->numberFormat('@');
		$format->bgColor('slateblue');

		$format = $this->_workbook->addStyle(Excel::STYLE_BLUE);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->border('All',1);
		$format->numberFormat('@');
		$format->bgColor('cornflowerblue');

		$format = $this->_workbook->addStyle(Excel::STYLE_ORANGE);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->border('All',1);
		$format->numberFormat('@');
		$format->bgColor('darkorange');

		$format = $this->_workbook->addStyle(Excel::STYLE_WHEAT);
		$format->alignHorizontal('Center');
		$format->alignVertical('Center');
		$format->alignWraptext();
		$format->fontSize(9);
		$format->fontBold();
		$format->border('All',1);
		$format->numberFormat('@');
		$format->bgColor('wheat');
	}
}

/* End of file excel.php */
/* Location: ./application/libraries/excel.php */